<?php
include($modRootPath.'lib/dbaFormmailer.o.inc.php');

if (!$usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','v'))
{
	print "<b>Sie haben nicht die entsprechenden Rechte, um diese Einstellung zu sehen!</b>";
	exit;
}


$fdba = new dbaFormmailer($udba);

$types  = $fdba->getFieldTplNames();
foreach ($types As $type)
{
    $typesByID[$type['id']] = $type['name'];
}
$checks = array('none'=>'keine','filledout'=>'ausgef&uuml;llt','email'=>'eMail','digit'=>'Zahlen','digitblank'=>'Zahlen und Leerzeichen','word'=>'Buchstaben','wordblank'=>'Buchstaben und Leerzeichen','alphanum'=>'Alphanumerisch','alphanumblank'=>'Alphanumerisch und Leerzeichen','plz'=>'PLZ','street'=>'Strasse');

switch ($env->getParam('action'))
{
    case 'add':
      if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','a'))
      {
        if ($check = $fdba->insertField($env->getParam('instance'),$env->getParam('name'),$env->getParam('type'),$env->getParam('value'),$env->getParam('description'),$env->getParam('checking'),$env->getParam('size'),$env->getParam('maxlength'),$env->getParam('selected')))
        {
            if ($check==-1) $emsg = "Der verwendete Name existiert bereits!";
        }
        else
        {
            $emsg = "Beim Speichern ist ein Fehler aufgetreten!: ".$udba->getError();
        }
      }
      else
      {
		print "<b>Sie haben nicht die entsprechenden Rechte, um Eintr&auml;ge hinzuzuf&uuml;gen!</b>";
		exit;
      }
        break;
    case 'move':
      if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','u'))
      {
        $fdba->exchangeFields($env->getParam('instance'),$env->getParam('name1'),$env->getParam('name2'));
      }
      else
      {
		print "<b>Sie haben nicht die entsprechenden Rechte, um Eintr&auml;ge zu ver&auml;ndern!</b>";
		exit;
      }
        break;
    case 'del':
      if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','d'))
      {
        if (!$fdba->deleteField($env->getParam('instance'),$env->getParam('name')))
        {
            $emsg = "Beim Speichern ist ein Fehler aufgetreten!: ".$udba->getError();
        }
      }
      else
      {
		print "<b>Sie haben nicht die entsprechenden Rechte, um Eintr&auml;ge zu l&ouml;schen!</b>";
		exit;
      }
        break;
    case 'uform':
        $field = $fdba->getField($env->getParam('instance'),$env->getParam('name'));
        break;
    case 'update':
      if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','u'))
      {
        if (!$fdba->updateField($env->getParam('instance'),$env->getParam('name'),$env->getParam('type'),$env->getParam('value'),$env->getParam('description'),$env->getParam('checking'),$env->getParam('size'),$env->getParam('maxlength'),$env->getParam('selected')))
        {
            $emsg = "Beim Speichern ist ein Fehler aufgetreten!: ".$udba->getError();
        }
      }
      else
      {
		print "<b>Sie haben nicht die entsprechenden Rechte, um Eintr&auml;ge zu ver&auml;ndern!</b>";
		exit;
      }
        break;
}

$fields = $fdba->getallFields($env->getParam('instance'));

?>
<form action="<?=$formaction?>" method="POST" name="dbform">
<input type="hidden" name="action" value="<?=(isset($field))?'update':'add'?>">
<input type="hidden" name="instance" value="<?=$instance?>">
<input type="hidden" name="module" value="<?=$module?>">
<input type="hidden" name="target" value="<?=$env->getParam('target')?>">
<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tr bgcolor="#3FB2EB">
    <td>Name</td>
    <td>Typ</td>
    <td>Wert(e)</td>
    <td>Beschreibung</td>
    <td>Pr&uuml;fmethode</td>
    <td>Gr&ouml;&szlig;e / Spalten</td>
    <td>Max.L&auml;nge / Zeilen</td>
    <td>Angew&auml;hlt</td>
</tr>
<tr>
    <td bgcolor="#E0F6FD">
        <?php if (isset($field) && $field['name']) echo '<input type="hidden" name="name" value="'.$field['name'].'" />'.$field['name']; else echo '<input type="text" name="name" size="10" maxlength="100" />'?>
    </td>
    <td bgcolor="#E0F6FD">
        <select name="type" class="button">
        <?php
            foreach ($types As $type)
            { ?>
            <option value="<?=$type['id']?>"<?=(isset($field) && $field['type']==$type['id'])?' selected="selected"':''?>><?=$type['name']?></option>
<?php 	    }
        ?>
        </select>
    </td>
    <td bgcolor="#E0F6FD">
        <input type="text" name="value" value="<?php echo (isset($field))?$field['value']:''?>" size="15" />
    </td>
    <td bgcolor="#E0F6FD">
        <input type="text" name="description" value="<?php echo (isset($field))?$field['description']:''?>" size="15" maxlength="255" />
    </td>
    <td bgcolor="#E0F6FD">
        <select name="checking" class="button">
        <?php
            foreach ($checks As $value => $name)
            { ?>
            <option value="<?=$value?>"<?=(isset($field) && $field['checking']==$value)?' selected="selected"':''?>><?=$name?></option>
<?php 	    }
        ?>
        </select>
    </td>
    <td bgcolor="#E0F6FD">
        <input type="text" name="size" value="<?php echo (isset($field))?$field['size']:''?>" size="3" maxlength="3" />
    </td>
    <td bgcolor="#E0F6FD">
        <input type="text" name="maxlength" value="<?php echo (isset($field))?$field['maxlength']:''?>" size="3" maxlength="3" />
    </td>
    <td bgcolor="#E0F6FD">
        <input type="checkbox" name="selected"<?=(isset($field) && $field['selected']=='Y')?' checked="checked"':''?> value="Y" size="3" maxlength="3" />
    </td>
</tr>
</table><br />
<?php if ((!isset($field) && $usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','a'))
		|| ($env->param('action') == 'uform' && $usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','u'))): ?>
<input type="submit" class="button" value="<?=(isset($field))?'&auml;ndern':'einf&uuml;gen'?>" />
<?php elseif ($env->param('action') == 'uform'):
		print '<input type="button" onClick="window.location.href=\''.$scriptname.'customizeForm.php\'" value="abbrechen" />';
	  endif; ?>
</form>
<br />
<h3>Formularfelder:</h3>
<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tr bgcolor="#3FB2EB">
    <td>Name</td>
    <td>Typ</td>
    <td>Wert(e)</td>
    <td>Beschreibung</td>
    <td>Pr&uuml;fmethode</td>
    <td>Gr&ouml;&szlig;e / Spalten</td>
    <td>Max.L&auml;nge / Zeilen</td>
    <td>Angew&auml;hlt</td>
    <td>Aktionen</td>
</tr>
<?php
for ($i=0;$i<count($fields);$i++):
?>
<tr>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$fields[$i]['name']?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$typesByID[$fields[$i]['type']]?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$fields[$i]['value']?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$fields[$i]['description']?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$checks[$fields[$i]['checking']]?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$fields[$i]['size']?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=$fields[$i]['maxlength']?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <?=($fields[$i]['selected']=='Y')?'Ja':'Nein'?></td>
    <td bgcolor="<?=($i%2==0)?'#E0F6FD':'#74DBFC'?>">
    <nobr><?php
    if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','u')): ?><input type="button" value="&auml;ndern" onClick="location.replace('<?=$scriptname.$env->getParam('target')?>&action=uform&name=<?=$fields[$i]['name']?>')" class="button" /> <?php endif; ?>
    <?php if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','d')): ?><input type="button" value="l&ouml;schen" onClick="location.replace('<?=$scriptname.$env->getParam('target')?>&action=del&name=<?=$fields[$i]['name']?>')" class="button" /> <?php endif; ?>
    <?php if ($usr->checkPermission($env->getParam('instance').'_mod_formmailer_form','u')): ?><input type="button" <?=($i==0)?'disabled="true" ':''?>value="hoch" onClick="location.replace('<?=$scriptname.$env->getParam('target')?>&action=move&name1=<?=$fields[$i]['name']?>&name2=<?php echo (isset($fields[$i-1]))?$fields[$i-1]['name']:''?>&<?=microtime()?>')" class="button" />
    <input type="button" <?=($i>count($fields)-2)?'disabled="true" ':''?>value="runter" onClick="location.replace('<?=$scriptname.$env->getParam('target')?>&action=move&name1=<?=$fields[$i]['name']?>&name2=<?php echo ($fields[$i+1])?$fields[$i+1]['name']:''?>&<?=microtime()?>')" class="button" /><?php endif; ?>
    </nobr></td>
</tr>
<?php
endfor;
?>
</table><br />

<?php
for ($i=0;$i<count($fields);$i++):
?>
<?php echo $fields[$i]['description'] ?>: #data[<?php echo $fields[$i]['name'] ?>]#<br/>
<?php
endfor;
?>

<script language="javascript">
<!-- 
  document.dbform.name.focus()
<?php
if (!empty($emsg)) print "alert('$emsg')\n";
?>
 //-->
</script>
